<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Returns a histogram operator for the specified data, with an optional
<span class='line'>  3</span>  * accessor function. If the data specified is not an array of numbers, an
<span class='line'>  4</span>  * accessor function must be specified to map the data to numeric values.
<span class='line'>  5</span>  *
<span class='line'>  6</span>  * @class Represents a histogram operator.
<span class='line'>  7</span>  *
<span class='line'>  8</span>  * @param {array} data an array of numbers or objects.
<span class='line'>  9</span>  * @param {function} [f] an optional accessor function.
<span class='line'> 10</span>  */</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="NAME">pv.histogram</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">frequency</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 13</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 14</span> 
<span class='line'> 15</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'> 16</span>      * Returns the computed histogram bins. An optional array of numbers,
<span class='line'> 17</span>      * &lt;tt>ticks&lt;/tt>, may be specified as the break points. If the ticks are
<span class='line'> 18</span>      * not specified, default ticks will be computed using a linear scale on the
<span class='line'> 19</span>      * data domain.
<span class='line'> 20</span>      *
<span class='line'> 21</span>      * &lt;p>The returned array contains {@link pv.histogram.Bin}s. The &lt;tt>x&lt;/tt>
<span class='line'> 22</span>      * attribute corresponds to the bin's start value (inclusive), while the
<span class='line'> 23</span>      * &lt;tt>dx&lt;/tt> attribute stores the bin size (end - start). The &lt;tt>y&lt;/tt>
<span class='line'> 24</span>      * attribute stores either the frequency count or probability, depending on
<span class='line'> 25</span>      * how the histogram operator has been configured.
<span class='line'> 26</span>      *
<span class='line'> 27</span>      * &lt;p>The {@link pv.histogram.Bin} objects are themselves arrays, containing
<span class='line'> 28</span>      * the data elements present in each bin, i.e., the elements in the
<span class='line'> 29</span>      * &lt;tt>data&lt;/tt> array (prior to invoking the accessor function, if any).
<span class='line'> 30</span>      * For example, if the data represented countries, and the accessor function
<span class='line'> 31</span>      * returned the GDP of each country, the returned bins would be arrays of
<span class='line'> 32</span>      * countries (not GDPs).
<span class='line'> 33</span>      *
<span class='line'> 34</span>      * @function
<span class='line'> 35</span>      * @name pv.histogram.prototype.bins
<span class='line'> 36</span>      * @param {array} [ticks]
<span class='line'> 37</span>      * @returns {array}
<span class='line'> 38</span>      */</span><span class="WHIT"> </span><span class="COMM">/** @private */</span><span class="WHIT">
<span class='line'> 39</span> </span><span class="WHIT">    </span><span class="NAME">bins</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">ticks</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 40</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pv.map</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bins</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 41</span> 
<span class='line'> 42</span> </span><span class="WHIT">      </span><span class="COMM">/* Initialize default ticks. */</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">arguments.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">ticks</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pv.Scale.linear</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">ticks</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 44</span> 
<span class='line'> 45</span> </span><span class="WHIT">      </span><span class="COMM">/* Initialize the bins. */</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="WHIT">      </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">ticks.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 47</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">bins</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 48</span> </span><span class="WHIT">        </span><span class="NAME">bin.x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ticks</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 49</span> </span><span class="WHIT">        </span><span class="NAME">bin.dx</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ticks</span><span class="PUNC">[</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">ticks</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 50</span> </span><span class="WHIT">        </span><span class="NAME">bin.y</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 51</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 52</span> 
<span class='line'> 53</span> </span><span class="WHIT">      </span><span class="COMM">/* Count the number of samples per bin. */</span><span class="WHIT">
<span class='line'> 54</span> </span><span class="WHIT">      </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">x.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 55</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">pv.search.index</span><span class="PUNC">(</span><span class="NAME">ticks</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 56</span> </span><span class="WHIT">            </span><span class="NAME">bin</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">bins</span><span class="PUNC">[</span><span class="NAME">Math.max</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">Math.min</span><span class="PUNC">(</span><span class="NAME">bins.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 57</span> </span><span class="WHIT">        </span><span class="NAME">bin.y</span><span class="PUNC">++</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 58</span> </span><span class="WHIT">        </span><span class="NAME">bin.push</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 60</span> 
<span class='line'> 61</span> </span><span class="WHIT">      </span><span class="COMM">/* Convert frequencies to probabilities. */</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">frequency</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">bins.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 63</span> </span><span class="WHIT">        </span><span class="NAME">bins</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x.length</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 65</span> 
<span class='line'> 66</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">bins</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 67</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 68</span> 
<span class='line'> 69</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'> 70</span>      * Sets or gets whether this histogram operator returns frequencies or
<span class='line'> 71</span>      * probabilities.
<span class='line'> 72</span>      *
<span class='line'> 73</span>      * @function
<span class='line'> 74</span>      * @name pv.histogram.prototype.frequency
<span class='line'> 75</span>      * @param {boolean} [x]
<span class='line'> 76</span>      * @returns {pv.histogram} this.
<span class='line'> 77</span>      */</span><span class="WHIT"> </span><span class="COMM">/** @private */</span><span class="WHIT">
<span class='line'> 78</span> </span><span class="WHIT">    </span><span class="NAME">frequency</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 79</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">arguments.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 80</span> </span><span class="WHIT">        </span><span class="NAME">frequency</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Boolean</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 81</span> </span><span class="WHIT">        </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 82</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 83</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">frequency</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 84</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 85</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 86</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 87</span> 
<span class='line'> 88</span> </span><span class="COMM">/**
<span class='line'> 89</span>  * @class Represents a bin returned by the {@link pv.histogram} operator. Bins
<span class='line'> 90</span>  * are themselves arrays containing the data elements present in the given bin
<span class='line'> 91</span>  * (prior to the accessor function being invoked to convert the data object to a
<span class='line'> 92</span>  * numeric value). These bin arrays have additional attributes with meta
<span class='line'> 93</span>  * information about the bin.
<span class='line'> 94</span>  *
<span class='line'> 95</span>  * @name pv.histogram.Bin
<span class='line'> 96</span>  * @extends array
<span class='line'> 97</span>  * @see pv.histogram
<span class='line'> 98</span>  */</span><span class="WHIT">
<span class='line'> 99</span> 
<span class='line'>100</span> </span><span class="COMM">/**
<span class='line'>101</span>  * The start value of the bin's range.
<span class='line'>102</span>  *
<span class='line'>103</span>  * @type number
<span class='line'>104</span>  * @name pv.histogram.Bin.prototype.x
<span class='line'>105</span>  */</span><span class="WHIT">
<span class='line'>106</span> 
<span class='line'>107</span> </span><span class="COMM">/**
<span class='line'>108</span>  * The magnitude value of the bin's range; end - start.
<span class='line'>109</span>  *
<span class='line'>110</span>  * @type number
<span class='line'>111</span>  * @name pv.histogram.Bin.prototype.dx
<span class='line'>112</span>  */</span><span class="WHIT">
<span class='line'>113</span> 
<span class='line'>114</span> </span><span class="COMM">/**
<span class='line'>115</span>  * The frequency or probability of the bin, depending on how the histogram
<span class='line'>116</span>  * operator was configured.
<span class='line'>117</span>  *
<span class='line'>118</span>  * @type number
<span class='line'>119</span>  * @name pv.histogram.Bin.prototype.y
<span class='line'>120</span>  */</span><span class="WHIT">
<span class='line'>121</span> </span></pre></body></html>